//
//  ECOrderAddViewController.m
//  ECERP.CRM
//
//  Created by Mac on 2018/10/19.
//  Copyright © 2018年 Mac. All rights reserved.
//

#import "ECOrderAddViewController.h"
#import "ECAddOrderTableViewCell.h"
#import "ECMemberAddTableViewCell.h"
#import "GLHTTPTool.h"
#import "ECOrderAddBottomToolView.h"
#import "ECOrderAddPhotoPickerTableViewCell.h"
#import "ECMemberListViewController.h"
#import "ECMemberLIstModel.h"
#import "ECSelectCommodityViewController.h"

@interface ECOrderAddViewController ()<UITableViewDelegate,UITableViewDataSource> {
    CGFloat imgCellHeight;
    ECMemberLIstModel * memberModel;
}

@property (nonatomic,strong) UITableView * tableView;

@property (nonatomic,strong) UILabel * sumLB;

@property (nonatomic,strong) NSMutableArray<NSMutableDictionary*> * baseInfo;

@property (nonatomic,strong) ECOrderAddBottomToolView * bottomToolView;

@property (nonatomic,strong) ECOrderAddPhotoPickerTableViewCell * imgPickCell;

@end

@implementation ECOrderAddViewController

- (void)viewDidLoad {
    self.navigationItem.title=@"新增订单";
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:nil];
    [super viewDidLoad];
    self.view.backgroundColor = ECCOLOR(0xf2f2f2);
    // Do any additional setup after loading the view.
}

- (void)loadSubView {
    imgCellHeight = 132 * PX_RELATIVE375;
    WS(weakSelf, self);
    [self.view addSubview:self.bottomToolView];
    [self.bottomToolView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(weakSelf.view);
        make.height.mas_equalTo(44 * PX_RELATIVE375);
        if (@available(iOS 11.0,*)) {
            make.bottom.mas_equalTo(weakSelf.view.mas_safeAreaLayoutGuideBottom);
        }else {
            make.bottom.mas_equalTo(weakSelf.view);
        }
    }];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.bottom.mas_equalTo(weakSelf.bottomToolView.mas_top);
    }];
    self.tableView.backgroundColor = ECCOLOR(0xF4F4F4);
    [self.view bringSubviewToFront:self.bottomToolView];
}

- (void)doAddShop {
    if (!memberModel) {
        [MBProgressHUD showText:@"请先选择客户" withTime:1];
    }else {
        ECSelectCommodityViewController * vc = [ECSelectCommodityViewController new];
        vc.customerLevelId = memberModel.customerLevelId;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.baseInfo.count + 1;
    }
    return 1;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return @"订单信息";
    }else {
        return @"商品信息";
    }
}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    view.backgroundColor = [UIColor clearColor];
    ((UITableViewHeaderFooterView*)view).textLabel.textColor = ECCOLOR(0xB3B3B3);
    ((UITableViewHeaderFooterView*)view).textLabel.font = ECFONT(13 * PX_RELATIVE375);
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    return @" ";
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row < self.baseInfo.count) {
            return 44*PX_RELATIVE375;
        }else {
            return imgCellHeight;
        }
    }
    return 98 * PX_RELATIVE375;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        ECMemberAddTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        if (indexPath.row < self.baseInfo.count) {
            NSMutableDictionary * dic = self.baseInfo[indexPath.row];
            cell.isNeed = [[dic objectForKey:@"isNeed"] boolValue];
            cell.cellStyle = [[dic objectForKey:@"style"] integerValue];
            cell.title = [dic objectForKey:@"title"];
            switch (cell.cellStyle) {
                case ECMemberAddCellStyleTxt:
                {
                    if (!dic[@"value"] || ((NSString*)dic[@"value"]).length < 1) {
                        cell.detailTF.placeholder = dic[@"placeholder"];
                    }else{
                        cell.detailTF.text = dic[@"value"];
                    }
                }
                    break;
                case ECMemberAddCellStyleMore: {
                    if (!dic[@"value"] || ((NSString*)dic[@"value"]).length < 1) {
                        cell.detailLB.text = dic[@"placeholder"];
                        cell.detailLB.textColor = ECCOLOR(0xcccccc);
                    }else{
                        cell.detailLB.text = dic[@"value"];
                        cell.detailLB.textColor = ECCOLOR(0x333333);
                    }
                }
                    break;
                case ECMemberAddCellStyleSwitch:{
                    if ([dic[@"value"] boolValue]) {
                        cell.isON = YES;
                    }else {
                        cell.isON = NO;
                    }
                }
                    break;
                default:
                    break;
            }
            if (indexPath.row == 1) {
                cell.accessoryImg = [UIImage imageNamed:@"order_add_date"];
            }
        }else {
            return self.imgPickCell;
        }
        return cell;
    }
    ECAddOrderTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.imgV.image = [UIImage imageNamed:@"order for goods_img"];
    cell.titleLB.text = @"澳洲龙虾";
    cell.priceLB.text = @"单价：¥99";
    cell.numLB.text = @"预定数量：9箱";
    cell.specificationLB.text = @"1-22.8公斤";
    cell.desLB.text = @"备注：这批货质量还可以";
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 0) {
        ECMemberListViewController * vc = [ECMemberListViewController new];
        WS(weakSelf, self);
        vc.cellSelectedBlock = ^(id obj) {
            self -> memberModel = obj;
            NSMutableDictionary * muDic = weakSelf.baseInfo.firstObject;
            [muDic setObject:((ECMemberLIstModel*)obj).customerName forKey:@"value"];
            [weakSelf.tableView reloadRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] withRowAnimation:UITableViewRowAnimationNone];
        };
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark LazyLoad

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [UITableView new];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.0001)];
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.0001)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionHeaderHeight = 27 * PX_RELATIVE375;
        _tableView.sectionFooterHeight = 0.00001 * PX_RELATIVE375;
        [_tableView registerClass:[ECMemberAddTableViewCell class] forCellReuseIdentifier:@"cell1"];
        [_tableView registerClass:[ECAddOrderTableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.bounces = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (UILabel *)sumLB {
    if (!_sumLB) {
        _sumLB = [UILabel new];
        _sumLB.textColor = ECCOLOR(0xff0000);
        _sumLB.font = ECFONT(14 * PX_RELATIVE375);
        _sumLB.textAlignment = NSTextAlignmentRight;
    }
    return _sumLB;
}

- (ECOrderAddBottomToolView *)bottomToolView {
    if (!_bottomToolView) {
        WS(weakSelf, self);
        _bottomToolView = [[ECOrderAddBottomToolView alloc] init];
        _bottomToolView.doAddBlock = ^{
            [weakSelf doAddShop];
        };
    }
    return _bottomToolView;
}


- (NSMutableArray<NSMutableDictionary *> *)baseInfo {
    if (!_baseInfo) {
        _baseInfo = @[
                      @{@"title":@"客户*",@"isNeed":@(YES),@"placeholder":@"请选择客户",@"style":@(ECMemberAddCellStyleMore),@"value":@""}.mutableCopy,
                      @{@"title":@"配送时间*",@"isNeed":@(YES),@"placeholder":[[NSDate date] stringWithFormat:@"yyyy-MM-dd"],@"style":@(ECMemberAddCellStyleMore),@"value":@""}.mutableCopy,
                      @{@"title":@"收件人",@"isNeed":@(NO),@"placeholder":@"请输入收件人姓名",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                      @{@"title":@"电话号码",@"isNeed":@(NO),@"placeholder":@"请输入电话号码",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                      @{@"title":@"收件地址",@"isNeed":@(NO),@"placeholder":@"请输入收件地址",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                      @{@"title":@"备注",@"isNeed":@(NO),@"placeholder":@"请输入备注",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                      ].mutableCopy;
    }
    return _baseInfo;
}

- (ECOrderAddPhotoPickerTableViewCell *)imgPickCell {
    if (!_imgPickCell) {
        _imgPickCell = [ECOrderAddPhotoPickerTableViewCell new];
        _imgPickCell.maxCout = 4;
        _imgPickCell.columnCount = 4;
        WS(weakSelf, self);
        _imgPickCell.changeHeightBlock = ^(CGFloat height) {
            self->imgCellHeight = height;
            [weakSelf.tableView reloadRow:weakSelf.baseInfo.count inSection:0 withRowAnimation:UITableViewRowAnimationNone];
        };
    }
    return _imgPickCell;
}

@end
